package com.jf.hotelreservation.demos.service.impl;

import com.alibaba.fastjson2.JSON;
import com.jf.hotelreservation.demos.bean.*;
import com.jf.hotelreservation.demos.bean.vo.HotelVo;
import com.jf.hotelreservation.demos.bean.vo.WXHotelVo;
import com.jf.hotelreservation.demos.mapper.HotelMapper;
import com.jf.hotelreservation.demos.mapper.OverviewMapper;
import com.jf.hotelreservation.demos.mapper.PCDMapper;
import com.jf.hotelreservation.demos.mapper.TagsMapper;
import com.jf.hotelreservation.demos.service.HotelService;
import com.jf.hotelreservation.demos.utils.DataUtils;
import com.jf.hotelreservation.demos.utils.PageBean;
import com.jf.hotelreservation.demos.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class HotelServiceImpl implements HotelService {

    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private OverviewMapper overviewMapper;
    @Autowired
    private TagsMapper tagsMapper;
    @Autowired
    private PCDMapper pcdMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String addHotel(HotelVo hotelVo) {
        //编辑功能
        if (hotelVo.getId() != 0){
            try{
                //编辑酒店信息
                hotelMapper.updateHotel(hotelVo);
                //删除关联详情标签
                overviewMapper.delOverviewByHid(hotelVo.getId());
                //新增关联详情标签
                int[] overviewList = hotelVo.getOverviewArr();
                for (int i : overviewList) {
                    hotelMapper.addHotelOverview(hotelVo.getId(),i);
                }
            }catch (Exception e){
                return JSON.toJSONString(R.error("编辑失败"));
            }

        //添加功能
        }else {
            try {
                hotelMapper.addHotel(hotelVo);
                int id = hotelVo.getId();
                int[] overviewList = hotelVo.getOverviewArr();
                for (int i : overviewList) {
                    hotelMapper.addHotelOverview(id,i);
                }
                return JSON.toJSONString(R.success());
            }catch (Exception e){
                return JSON.toJSONString(R.error("添加失败"));
            }
        }
      return null;
    }


    @Override
    public String queryAllHotel(Integer currentPage, Integer pageSize) {
        List<HotelVo> hotelVoList = new ArrayList<>();
        List<Hotel> hotelList = hotelMapper.queryAll(DataUtils.getStartRecord(currentPage, pageSize), pageSize);
        for (Hotel hotel : hotelList) {
            HotelVo hotelVo = new HotelVo();
            BeanUtils.copyProperties(hotel,hotelVo);
            List<Overview> overviewList = overviewMapper.queryByHotelId(hotel.getId());
            Tags tags = tagsMapper.queryByHotelId(hotel.getTagsId());
            hotelVo.setTag(tags);
            hotelVo.setOverviewList(overviewList);
            hotelVoList.add(hotelVo);
        }
        long count = hotelMapper.count();
        PageBean pageBean = new PageBean();
        pageBean.setRows(hotelVoList);
        pageBean.setTotal(count);
        return JSON.toJSONString(R.success("操作成功",pageBean));
    }

    @Override
    public String queryById(int id) {
        HotelVo hotelVo = new HotelVo();
        Hotel hotel = hotelMapper.queryById(id);
        Tags tags = tagsMapper.queryByHotelId(hotel.getTagsId());
        List<Overview> overviewList = overviewMapper.queryByHotelId(id);
        Province provinceByHid = pcdMapper.getProvinceByHid(hotel.getProvince());
        Cities cityByHid = pcdMapper.getCityByHid(hotel.getCity());
        Areas areasByHid = pcdMapper.getAreasByHid(hotel.getArea());
        BeanUtils.copyProperties(hotel,hotelVo);
        hotelVo.setOverviewList(overviewList);
        hotelVo.setTag(tags);
        hotelVo.setAreas(areasByHid);
        hotelVo.setProvinceBean(provinceByHid);
        hotelVo.setCities(cityByHid);
        return JSON.toJSONString(R.success("操作成功",hotelVo));
    }

    @Override
    public String queryByRoom() {
        return JSON.toJSONString(R.success(hotelMapper.queryAllByRoom()));
    }

    @Override
    public String queryByCity(Integer cityCode) {
        List<WXHotelVo> wxHotelVoList = new ArrayList<>();
        List<Hotel> hotelList = hotelMapper.queryByCity(cityCode);
        for (Hotel hotel : hotelList) {
            WXHotelVo wxHotelVo = new WXHotelVo();
            BeanUtils.copyProperties(hotel,wxHotelVo);
            Tags tags = tagsMapper.queryByHotelId(hotel.getTagsId());
            List<Overview> overviewList = overviewMapper.queryByHotelId(hotel.getId());
            wxHotelVo.setTags(tags);
            wxHotelVo.setOverviewList(overviewList);
            wxHotelVo.setPrice(299);
            wxHotelVoList.add(wxHotelVo);
        }

        return JSON.toJSONString(R.success(wxHotelVoList));
    }
}
